<?php
/**
 * Notes:
 * User: sk
 * Date: 2020/9/27
 * Time: 18:11
 * ${PARAM_DOC}
 * @return ${TYPE_HINT}
 * ${THROWS_DOC}
 */

namespace App\Http\model;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Dotenv\Exception;
use Illuminate\Support\Facades\Log;


class Getorder extends Model

{
    /**
     * 【下载订单接口】
     * author   sk
     * @param $PageIndex   第几页
     * @param $PageSize    每页多少条数据
     * @return mixed       返回数据
     */
    public function GetOrderModel($numtotalorder, $res)
    {
//        Log::info('传过来的订单数据' . json_encode($res));

        foreach ($res as $k) {
            $event = json_decode(json_encode($k), true);
            $orders['PlatOrderNo'] = $event['OIcode'];//订单号
//            dd($event);
            //订单状态Status==0 拼团订单  99正常订单
            if ($event['Status'] == 0 && $event['orStatus'] == 0) {//订单状态Status==0 拼团订单
                $orders['tradeStatus'] = 'JH_01';//代付款
            }

            if ($event['Status'] == 0 && $event['orStatus'] == 1) {
                $grpid = DB::table('orders')->where('Code', $event['OrderCode'])->first(['grpid']);
                $usergroupon = DB::table('usergroupon')->where('gid', $grpid->grpid)->first(['status']);
                $usergroupon = json_decode(json_encode($usergroupon), true);
                if ($usergroupon['status'] == 1) {
                    //拼团成功
                    $orders['tradeStatus'] = 'JH_02';//待发货
                }
            }
            if ($event['Status'] == 0 && $event['exprestate'] == 1) {
                $grpid = DB::table('orders')->where('Code', $event['OrderCode'])->first(['grpid']);
                $usergroupon = DB::table('usergroupon')->where('gid', $grpid->grpid)->first(['status']);
                $usergroupon = json_decode(json_encode($usergroupon), true);
                if ($usergroupon['status'] == 1) {
                    $orders['tradeStatus'] = 'JH_03';//待收货
                }
            }
            if ($event['Status'] == 0 && $event['orStatus'] == 9) {

                $grpid = DB::table('orders')->where('Code', $event['OrderCode'])->first(['grpid']);
                $usergroupon = DB::table('usergroupon')->where('gid', $grpid->grpid)->first(['status']);
                $usergroupon = json_decode(json_encode($usergroupon), true);
                if ($usergroupon['status'] == 1) {
                    $orders['tradeStatus'] = 'JH_04';//9确认收货==已完成
                }
            }
            if ($event['Status'] == 0 && $event['orStatus'] == 6) {
                $grpid = DB::table('orders')->where('Code', $event['OrderCode'])->first(['grpid']);
                $usergroupon = DB::table('usergroupon')->where('gid', $grpid->grpid)->first(['status']);
                $usergroupon = json_decode(json_encode($usergroupon), true);
                if ($usergroupon['status'] == 1) {
                    $orders['tradeStatus'] = 'JH_05';//交易关闭
                }

            }

            if ($event['Status'] == 99 && $event['orStatus'] == 0) {//订单状态
                $orders['tradeStatus'] = 'JH_01';//代付款
            }
            if ($event['Status'] == 99 && $event['orStatus'] == 1) {
                $orders['tradeStatus'] = 'JH_02';//待发货
            }
            if ($event['Status'] == 99 && $event['exprestate'] == 1) {
                $orders['tradeStatus'] = 'JH_03';//待收货
            }
            if ($event['Status'] == 99 && $event['orStatus'] == 9) {
                $orders['tradeStatus'] = 'JH_04';//9确认收货==已完成
            }
            if ($event['Status'] == 99 && $event['orStatus'] == 6) {
                $orders['tradeStatus'] = 'JH_05';//交易关闭
            }
            if ($event['Status'] == 99 && $event['orStatus'] == 6) {
                $orders['tradeStatus'] = 'JH_05';//交易关闭
            }

            if ($event['Status'] == 50) {
                $orders['tradeStatus'] = 'JH_05';//买家已经申请退款等待卖家同意=JH_01
            }
            if ($event['Status'] == 59 || $event['Status'] == 79) {
                $orders['tradeStatus'] = 'JH_05';//退款成功
            }
            if ($event['Status'] == 52) {
                $orders['tradeStatus'] = 'JH_05';//拒绝退款
            }

            $orders['tradetime'] = $event['OrderTime'];
//dd($orders);
            //根据子订单查看主订单的GroupCode
            $GroupCode = DB::table('orders')->where('Code', $k->OrderCode)->first(['GroupCode', 'PayDate', 'PayType']);
//            dd($GroupCode);
            $provinceData = DB::table('receiveinfo')
                ->leftJoin('province', 'receiveinfo.Province', '=', 'province.Code')
                ->where('OrderGroupCode', $GroupCode->GroupCode)
                ->select('receiveinfo.OrderGroupCode',
                    'receiveinfo.Province',// 省   province
                    'receiveinfo.City',//市  city
                    'receiveinfo.Area',//区县   area
                    'receiveinfo.Address',//详细地址   address
                    'receiveinfo.Telphone',//电话 phone 和 mobile
                    'receiveinfo.Consignee',//收货人姓名 receivername  和 昵称
                    'province.Name'//province 州/省
                )->first();
            $provinceData = json_decode(json_encode($provinceData), true);
//            dd($provinceData);
//            Log::info('$provinceData'.json_encode($provinceData));
            $province = json_decode(json_encode($provinceData), true);
            $cityName = DB::table('city')->select('Name')->where('Code', $province['City'])->first();
            $cityName = json_decode(json_encode($cityName), true);

            $AreaName = DB::table('area')->select('Name')->where('Code', $province['Area'])->first();
            $AreaName = json_decode(json_encode($AreaName), true);

            $detail = DB::table('orderdetail')
                ->select('orderdetail.OrderCode', 'orderdetail.OIcode', 'orderdetail.SKU_ID', 'orderdetail.ProductName as tradegoodsname', 'orderdetail.ProductCount as goodscount',//商品数量
                    'orderdetail.ProductPrice as ProductPrice',//购买是的单价
                    'orderdetail.ProductNum', 'allpayprice', 'payprice', 'cpprice'
                )->where('orderdetail.OIcode', $event['OIcode'])
                ->first();
//            $details = json_decode(json_encode($detail), true);
//            dd($details);

            $orders['province'] = $province['Name'];
            $orders['city'] = $cityName['Name'];
            $orders['area'] = $AreaName['Name'];
            $orders['phone'] = $province['Telphone'];
            $orders['mobile'] = $province['Telphone'];
            $orders['goodsfee'] = $detail->allpayprice;//货款金额
            $orders['totalmoney'] = $detail->payprice;//应收合计
            $orders['favourablemoney'] = $detail->cpprice;//优惠金额


            $orders['paytime'] = $GroupCode->PayDate;
            $orders['receivername'] = $province['Consignee'];
            $orders['address'] = $provinceData['Address'];
            $nick = DB::table('users')->where('ID', $event['BuyerID'])->select('NickName')->first();
            $nick = json_decode(json_encode($nick), true);
            $orders['nick'] = $nick['NickName'];
            $orders['ShouldPayType'] = $GroupCode->PayType;
            $orders['logisticno'] = $event['expressnum'];
            $orders['logisticname'] = $event['expressname'];

//dd($orders);
            $list = DB::table('shopsku as sh')
                ->leftJoin('sku as s', 'sh.skuid', '=', 's.id')
                ->select('sh.stock', 'sh.price', 'sh.discountprice', 's.specification', 'sh.skuid', 's.specification', 's.name', 's.num')
                ->where('sh.id', '=', $detail->SKU_ID)
                ->first();
            $temp['ProductId'] = $list->skuid;
            $temp['suborderno'] = $detail->OIcode;

            $temp['tradegoodsno'] = $list->num;
            $temp['tradegoodsname'] = $detail->tradegoodsname;
            $temp['tradegoodsspec'] = $list->specification;
            $temp['goodscount'] = $detail->goodscount;
            $temp['price'] = $detail->ProductPrice;
            $orders['goodinfos'] = [$temp];
            $arr[] = $orders;
        }

        $array['code'] = 10000;
        $array['message'] = 'SUCCESS';
        $array['numtotalorder'] = $numtotalorder;
        $array['orders'] = isset($arr) ? $arr : [];
        return $array;
    }


    public function GetSkuids_data()
    {
        $GetSkuids_DB = DB::table('shopsku as sh');
        $GetSkuids_DB = $GetSkuids_DB
            ->join('sku as sk', 'sh.skuid', '=', 'sk.id');

        $where = ['sk.skutype' => 3, 'sk.coupon' => null];//吉客云不需要的订单的商品
        $select = ['sh.id'];
        $GetSkuids_Data = $GetSkuids_DB
            ->where($where)
            ->where('sh.price', '<=', 0)
            ->select($select)
            ->get()
            ->toArray();
        $GetSkuids_Data_array = array_column($GetSkuids_Data, 'id');

        $result = array_unique($GetSkuids_Data_array);
        return $result;


    }

}
